package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Cv;
import com.zpark.resume.entity.Member;
import com.zpark.resume.entity.Recommend;
import com.zpark.resume.exception.*;
import com.zpark.resume.mapper.*;
import com.zpark.resume.service.IMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: MemberServiceImpl
 * @Description: 会员业务层实现类
 * @author: RZS
 * @date: 2023/5/30  15:46
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private IMemberMapper memberMapper;
    @Autowired
    private ICvMapper cvMapper;
    @Autowired
    private IRecommendMapper recommendMapper;

    /**
     * 会员登录
     * @param username 用户名
     * @param password 密码
     * @return 会员对象
     */
    @Override
    public Member login(String username, String password) {
        // 1、判断username和password是否为空，
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            // 如果为空则，抛出异常UsernameOrPasswordIsNullException
            throw new UsernameOrPasswordIsNullException("用户名或者密码为空异常");
        }
        // 2、不为空，则调用持久层接口查询数据
        Member member = memberMapper.selectByUsername(username);
        // 3、判断查询结果是否为空，
        if (member == null) {
            // 如果为空，则抛出异常MemberIsNullException
            throw new MemberIsNullException("查询的用户不存在异常");
        }
        // 4、如果查询结果不为空，则判断密码
        if (!password.equals(member.getPsword())) {
            // 5、如果密码不一致，则抛出异常PasswordErrorException
            throw new PasswordErrorException("密码错误异常");
        }
        // 6、密码一致，则返回数据
        return member;
    }

    /**
     * 用户注册功能
     * @param member
     * @return
     */
    @Override
    public Integer reg(Member member) {
        // 1、判断username和password是否为空，判断member是否为null
        if (member == null || StringUtils.isBlank(member.getUsname()) || StringUtils.isBlank(member.getPsword())) {
            // 如果为空，则注册失败，抛出异常MemberIsNullOrUsernameOrPasswordIsNullException
            throw new MemberIsNullOrUsernameOrPasswordIsNullException("member对象为null或者用户名密码为空异常");
        }

        // 2、不为空，根据用户名查询用户数据，判断用户是否存在
        Member selectByUsername = memberMapper.selectByUsername(member.getUsname());

        if (selectByUsername != null) {
            // 如果存在，注册失败，抛出异常MemberExistRegErrorException
            throw new MemberExistRegErrorException("会员名字已经被注册");
        }
        // 完善member数据
        member.setCreateTime(new Date());
        // 3、不存在，调用持久层接口实现注册
        Integer row = memberMapper.insertMember(member);
        // 4、判断是否注册失败，如果注册失败，则抛出异常MemberRegErrorException
        if (row <= 0) {
            // 注册失败，则抛出异常MemberRegErrorException
            throw new MemberRegErrorException("注册会员失败");
        }

        // 5、调用ICvMapper执行插入操作   member_id、name、created_time
        Cv cv = new Cv();
        cv.setMemberId(member.getId());
        cv.setName(member.getUsname());
        cv.setCreatedTime(new Date());
        cv.setGender(0);
        cv.setPoliticsStatus(13);
        cv.setMaritalStatus(0);
        cv.setDesiredStatus(0);
        cv.setStatus(0);
        cvMapper.insertCv(cv);

        // 6、调用IRecommendMapper执行插入操作   member_id、created_time
        Recommend recommend = new Recommend();
        recommend.setMemberId(member.getId());
        recommend.setCreatedTime(new Date());
        recommend.setStatus(0);
        recommendMapper.insertRecommend(recommend);

        return row;
    }


    /**
     * 根据id查询mrmber
     * @param id
     * @return
     */
    @Override
    public Member findById(Integer id) {
        // 调用持久层接口查询数据
        Member member = memberMapper.selectById(id);
        // 判断查询结果
        if (member == null) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return member;
    }
}
